package cn.jhc.teachingquality.fragment;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;

import cn.jhc.teachingquality.Global;
import cn.jhc.teachingquality.R;
import cn.jhc.teachingquality.bean.PracticeDetail;
import cn.jhc.teachingquality.bean.PracticeLog;
import cn.jhc.teachingquality.net.RequestQueueFactory;
import cn.jhc.teachingquality.net.practice.PracticeDetailRequest;
import cn.jhc.teachingquality.net.practice.SavePracticeLogRequest;

/**
 * Created by luyanfei on 16-3-16.
 */
public class PracticeLogFragment extends Fragment
        implements View.OnClickListener{

    private PracticeLog practiceLog;
    private PracticeDetail practiceDetail;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View viewHierarchy = inflater.inflate(R.layout.practice_log_detail, container, false);
        Bundle args = getArguments();
        this.practiceLog = (PracticeLog)args.getSerializable(Global.CURRENT_PRACTICE_LOG);

        TextView titleView = (TextView)viewHierarchy.findViewById(R.id.txt_practice_title);
        titleView.setText(practiceLog.getTitle());
        TextView authorView = (TextView)viewHierarchy.findViewById(R.id.txt_student_week);
        authorView.setText(practiceLog.getStudent() + "(第" + practiceLog.getWeekSeq() + "周)");

        final TextView contentView = (TextView)viewHierarchy.findViewById(R.id.txt_practice_content);
        final EditText remarkText = (EditText)viewHierarchy.findViewById(R.id.edit_remark);
        //发送请求
        PracticeDetailRequest detailRequest = new PracticeDetailRequest(practiceLog.getHiddenId(),
                new Response.Listener<PracticeDetail>() {
                    @Override
                    public void onResponse(PracticeDetail response) {
                        if(response == null){
                            Toast.makeText(getContext(),"无法抽取日志内容。", Toast.LENGTH_LONG);
                        }else {
                            contentView.setText(response.getContent());
                            remarkText.setText(response.getRemark());
                            //keep response for save practice log usage.
                            practiceDetail = response;
                        }
                    }
                });
        RequestQueueFactory.getRequestQueue(getActivity()).add(detailRequest);

        Button saveButton = (Button) viewHierarchy.findViewById(R.id.btn_save_remark);
        saveButton.setOnClickListener(this);


        return viewHierarchy;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //FloatingActionButton
        FloatingActionButton fab = (FloatingActionButton)getView().findViewById(R.id.fab_email);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.setType("text/plain; charset=utf8");
                emailIntent.putExtra(Intent.EXTRA_TITLE, practiceLog.getTitle());
                String subject = String.format(getResources().getString(R.string.recommend_subject),
                        practiceLog.getTitle(), practiceLog.getStudent(), practiceLog.getWeekSeq());
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
                String content = String.format(getResources().getString(R.string.recommend_content),
                        practiceLog.getStudent(), "", "", practiceDetail.getContent());
                emailIntent.putExtra(Intent.EXTRA_TEXT, content);
                try {
                    startActivity(emailIntent);
                }catch (ActivityNotFoundException exception){
                    //安卓上没有处理email的应用
                    Toast.makeText(getContext(),
                            getResources().getString(R.string.no_email_agent_found),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        //read remark from edittext.
        EditText remarkText = (EditText)getView().findViewById(R.id.edit_remark);
        String newRemark = remarkText.getText().toString();
        if(practiceDetail.getRemark().equals(newRemark)){
            Toast.makeText(getContext(), "没有改动，无需保存。", Toast.LENGTH_SHORT);
            return;
        }

        final Button button = (Button) v;
        button.setClickable(false);

        final ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.save_progress_bar);
        progressBar.setVisibility(View.VISIBLE);

        practiceDetail.setRemark(newRemark);
        SavePracticeLogRequest saveRequest = new SavePracticeLogRequest(practiceLog, practiceDetail,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        button.setClickable(true);
                        Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
                        progressBar.setVisibility(View.GONE);
                    }
                });
        RequestQueueFactory.getRequestQueue(getActivity()).add(saveRequest);
    }
}
